package com.sanron.sunweather.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanron.sunweather.R;

public class PetalView extends RelativeLayout{

	private static Bitmap background;//正常状态
	private static Bitmap background_pressed;//按下状态
	private static BitmapFactory.Options opts;
	private ImageView ivWeather;//天气图标
	private TextView tvTemperature;//温度
	private TextView tvWeekDay;//星期
	
	public PetalView(Context context){
		this(context,null);
	}

	public PetalView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		if(opts == null){
			opts = new BitmapFactory.Options();
			opts.inJustDecodeBounds = true;
			BitmapFactory.decodeResource(getResources(),R.drawable.petal,opts);
		}
		
		ivWeather = new ImageView(getContext());
		tvTemperature = new TextView(getContext());
		tvWeekDay = new TextView(getContext());

		tvTemperature.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
		tvTemperature.setTextColor(Color.WHITE);
		
		tvWeekDay.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
		tvWeekDay.setTextColor(Color.WHITE);
	
		ivWeather.setScaleType(ScaleType.CENTER);
		
		ivWeather.setId(1001);
		tvTemperature.setId(1002);
		tvWeekDay.setId(1003);
		
		LayoutParams lpTemperature = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		LayoutParams lpWeather = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		LayoutParams lpWeekDay = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		
		lpTemperature.addRule(RelativeLayout.CENTER_IN_PARENT);
		
		lpWeather.addRule(RelativeLayout.CENTER_HORIZONTAL);
		lpWeather.addRule(RelativeLayout.ABOVE,tvTemperature.getId());
		
		lpWeekDay.addRule(RelativeLayout.CENTER_HORIZONTAL);
		lpWeekDay.addRule(RelativeLayout.BELOW,tvTemperature.getId());
		lpWeekDay.topMargin = (int) (getResources().getDisplayMetrics().density*5);
		addView(tvTemperature,lpTemperature);
		addView(ivWeather,lpWeather);
		addView(tvWeekDay,lpWeekDay);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		int measureWidth = opts.outWidth*getMeasuredHeight()/opts.outHeight;
		setMeasuredDimension(measureWidth,getMeasuredHeight());
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);

		if(Build.VERSION.SDK_INT<Build.VERSION_CODES.LOLLIPOP){
			//安卓5.0以下需要生成两个不同透明度位图
			if(background==null){
				//载入背景位图
				Bitmap origin = BitmapFactory.decodeResource(getResources(),R.drawable.petal);
				background = Bitmap.createBitmap(getWidth(), getHeight(),Config.ARGB_8888);
				background_pressed = Bitmap.createBitmap(getWidth(), getHeight(),Config.ARGB_8888);
				Canvas canvas = new Canvas();
				
				canvas.setBitmap(background);
				Paint paint = new Paint();
				paint.setAlpha(0x90);
				canvas.drawBitmap(origin,null,new Rect(0,0,getWidth(),getHeight()),paint);
				
				canvas.setBitmap(background_pressed);
				paint.setAlpha(0x40);
				canvas.drawBitmap(origin,null,new Rect(0,0,getWidth(),getHeight()),paint);
	
				origin.recycle();
				System.gc();
			}
			if(getBackground()==null){
				BitmapDrawable normal = new BitmapDrawable(getResources(),background);
				BitmapDrawable pressed = new BitmapDrawable(getResources(),background_pressed);
				StateListDrawable drawable = new StateListDrawable();
				drawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
				drawable.addState(new int[]{}, normal);
				setBackground(drawable);
			}
		}else{
			//安卓5.0以上只需载入一个位图，可以设置不同透明度作为Drawable
			if(background==null){
				//载入背景位图
				Bitmap origin = BitmapFactory.decodeResource(getResources(),R.drawable.petal);
				background = Bitmap.createScaledBitmap(origin,getWidth(),getHeight(),true);
				origin.recycle();
				System.gc();
			}
			if(getBackground()==null){
				BitmapDrawable normal = new BitmapDrawable(getResources(),background);
				normal.setAlpha(0x90);
				BitmapDrawable pressed = new BitmapDrawable(getResources(),background);
				pressed.setAlpha(0x40);
				StateListDrawable drawable = new StateListDrawable();
				drawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
				drawable.addState(new int[]{}, normal);
				setBackground(drawable);
			}
		}
	}

	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int x = (int) event.getX();
		int y = (int) event.getY();
		
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			if((x>0&&x<background.getWidth()&&y>0&&y<background.getHeight())
					&&background.getPixel(x, y)==Color.TRANSPARENT){
				//限定不透明区域为可点击，模拟不规则控件
				return false;
			}
		}
		
		return super.onTouchEvent(event);
	}
	
	public ImageView getWeatherView(){
		return ivWeather;
	}
	
	public TextView getTemperatureView(){
		return tvTemperature;
	}
	
	public TextView getWeekDayView(){
		return tvWeekDay;
	}
	
	
	
}
